package study.spring.core.ioc._3_bean_assemble.propertyAssemble;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.stereotype.Component;

@Component
public class CustomBeanPostProcessor implements BeanFactoryPostProcessor {

  @Override
  public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    GenericBeanDefinition definition = (GenericBeanDefinition) beanFactory.getBeanDefinition("injectWithSetMethodInjection");
    int autowireMode1 = definition.getAutowireMode();
    System.out.println(autowireMode1);
    definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
    // 不论是设置为 BY_NAME 还是 BY_TYPE 都会寻找到 setter 方法装配

    definition = (GenericBeanDefinition) beanFactory.getBeanDefinition("injectWithFieldInjection");
    int autowireMode = definition.getAutowireMode();
    System.out.println(autowireMode);
  }
}
